<template>
	<view class="page">
		<!-- 头部 -->
		<view class="header">
			<view class="header-content">
				<view class="header-top">
					<view class="header-left">
						<text class="header-title">任务列表</text>
						<text class="badge status-badge">进行中</text>
					</view>
					<text class="completion-text">
						完成度: <text class="completion-value">{{ completionRate }}%</text>
					</text>
				</view>
				<view class="progress-container">
					<view class="progress-bar" :style="{ width: completionRate + '%' }"></view>
				</view>
			</view>
		</view>

		<!-- 主内容 -->
		<view class="main-content">
			<!-- 概览卡片 -->
			<view class="card">
				<view class="card-content">
					<view class="card-left">
						<text class="card-title">项目进度概览</text>
						<text class="card-subtitle">
							{{ taskList.length }}项任务中已完成{{ completedCount }}项，剩余{{ taskList.length - completedCount }}项待完成
						</text>
					</view>
					<view class="card-stats">
						<view class="stat-item">
							<text class="stat-value completed-value">{{ completedCount }}</text>
							<text class="stat-label">已完成</text>
						</view>
						<view class="stat-item">
							<text class="stat-value inprogress-value">{{ taskList.length - completedCount }}</text>
							<text class="stat-label">进行中</text>
						</view>
					</view>
				</view>
			</view>


			<!-- 任务项列表 -->
			<!-- 修改任务项列表部分 -->
			<view class="task-list">
				<view v-for="(task, index) in taskList" :key="task.taskId" class="task-item" :class="{
			        'completed-task': task.status === 3
			    }">
					<view class="task-content">
						<view class="task-header">
							<text class="task-title" :class="{ 'completed-text': task.status === 3 }">
								{{ task.content }}
							</text>
							<text class="badge" :class="{
			                        'badge-done': task.status === 3,
			                        'badge-required': task.status !== 3 && task.status !== 0,
			                        'badge-optional': task.status === 0
			                    }">
								{{ getStatusText(task.status) }}
							</text>
						</view>
						<text class="task-deadline">
							截止时间：{{ formatDate(task.endtime) }}
						</text>
						<view class="task-progress">
							<view class="progress-bg">
								<view class="progress-fill" :class="{
			                            'progress-done': task.status === 3,
			                            'progress-required': task.status !== 3 && task.status !== 0,
			                            'progress-optional': task.status === 0
			                        }" :style="{ width: task.status === 3 ? '100%' : '0%' }"></view>
							</view>
							<text class="progress-text">{{ task.status === 3 ? '100%' : '0%' }}</text>
						</view>
					</view>
					<!-- 修改后的按钮 -->
					<button class="action-button" @click="navigateToSignIn(task)" :disabled="task.status === 3">
						{{ task.status === 3 ? '已完成' : '前往' }}
					</button>
				</view>
			</view>
			<!-- 创建清单按钮 -->
			<view class="create-btn-wrap">
				<button class="btn-secondary" @tap="goToCreateList">创建清单</button>
				<button class="btn-secondary" @tap="openpxfk">培训反馈</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getChildTaskList,
		makeSureJob
	} from "../../utils/getDate.js"

	export default {
		data() {
			return {
				taskList: [],
				projectId: ""
			}
		},
		computed: {
			completedCount() {
				return this.taskList.filter(task => task.status === 3).length;
			},
			completionRate() {
				if (this.taskList.length === 0) return 0;
				return Math.round((this.completedCount / this.taskList.length) * 100);
			}
		},
		onLoad(options) {
			this.projectId = options.projectId;
		},
		onShow() {
			if (this.projectId) {
				this.ongetChildTaskList();
			}
		},
		methods: {
			formatDate(dateString) {
				if (!dateString) return '';
				const date = new Date(dateString);
				return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
			},
			openpxfk() {
				uni.navigateTo({
					url: `/pages/TC/feedback?projectId=${this.projectId}`
				});
			},
			getStatusText(status) {
				switch (status) {
					case 3:
						return '已完成';
					case 2:
						return '已逾期';
					case 1:
						return '进行中';
					case 0:
						return '未开始';
					default:
						return '必须完成';
				}
			},
			navigateToSignIn(task) {
				console.log(task, "task")
				if (task.task_type == "1") {
					let data = {
						taskId: task.taskId,
						imgurl: "",
						number: "",
						content: "",
						note: "",
						createId: uni.getStorageSync('teacher').id
					}
					makeSureJob(data).then(res => {
						if (res.state === "ok") {
							this.ongetChildTaskList();
						} else {
							uni.showToast({
								title: '打卡成功',
								icon: 'none'
							});
						}
					}).catch(err => {
						console.error('打卡失败:', err);
						uni.showToast({
							title: '打卡失败',
							icon: 'none'
						});
					});


				} else {
					uni.navigateTo({
						url: `/pages/TC/TCsignIn?taskId=${task.taskId}`
					});
				}

			},

			ongetChildTaskList() {
				const data = {
					projectId: this.projectId
				};
				getChildTaskList(data).then(res => {
					if (res.state === 'ok') {
						this.taskList = res.data;
					} else {
						uni.showToast({
							title: '获取任务失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error(err);
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					});
				});
			},
			goToCreateList() {


				uni.navigateTo({
					url: `/pages/TC/CreateList?projectId=${this.projectId}`
				});
			}
		}
	}
</script>

<style scoped>
	/* 基础样式 */
	.page {
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
		padding-bottom: 20px;
	}

	/* 新增/修改的样式 */
	.action-button {
		min-width: 80px;
		height: 36px;
		border-radius: 18px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 16px;
		border: none;
		outline: none;
		position: relative;
		overflow: hidden;
		transition: all 0.3s ease;
		font-size: 14px;
		font-weight: 500;
		padding: 0 16px;
	}

	/* 未完成状态按钮样式 */
	.action-button:not([disabled]) {
		background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
		color: white;
		box-shadow: 0 2px 6px rgba(79, 70, 229, 0.3);
	}

	/* 完成状态按钮样式 */
	.action-button[disabled] {
		background: #e5e7eb;
		color: #9ca3af;
		cursor: not-allowed;
	}

	/* 按钮悬停效果 */
	.action-button:not([disabled]):hover {
		transform: translateY(-2px);
		box-shadow: 0 4px 8px rgba(79, 70, 229, 0.4);
	}

	/* 按钮点击效果 */
	.action-button:active:not([disabled]) {
		transform: scale(0.98);
		box-shadow: 0 2px 4px rgba(79, 70, 229, 0.3);
	}

	/* 移除原来的按钮样式 */
	.check-button,
	.icon,
	.icon-check,
	.icon-check-circle {
		display: none;
	}

	/* 头部样式 */
	.header {
		color: white;
		background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
	}

	.create-btn-wrap {
		margin-top: 40rpx;
		display: flex;
		justify-content: flex-end;
	}

	.btn-secondary {
		background-color: #6B3FEB;
		color: #fff;
		font-size: 28rpx;
		padding: 16rpx 40rpx;
		border-radius: 12rpx;
	}

	.header-content {
		margin: 0 auto;
		padding: 16px 24px;
		box-sizing: border-box;
	}

	.header-top {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 12px;
	}

	.header-left {
		display: flex;
		align-items: center;
	}

	.header-title {
		font-size: 18px;
		font-weight: bold;
	}

	.status-badge {
		margin-left: 12px;
		padding: 2px 8px;
		border-radius: 12px;
		background-color: rgba(255, 255, 255, 0.2);
		font-size: 12px;
	}

	.completion-text {
		font-size: 14px;
	}

	.completion-value {
		font-weight: 600;
	}

	.progress-container {
		height: 4px;
		width: 100%;
		background-color: rgba(255, 255, 255, 0.3);
		border-radius: 2px;
		overflow: hidden;
	}

	.progress-bar {
		height: 100%;
		background: linear-gradient(90deg, #4f46e5 0%, #7c3aed 100%);
	}

	/* 主内容区域 */
	.main-content {
		margin: 0 auto;
		padding: 50rpx 40rpx;
		box-sizing: border-box;

	}

	/* 卡片样式 */
	.card {
		background-color: #fff;
		border-radius: 12px;
		padding: 20px;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
		border: 1px solid #e5e7eb;
		margin-bottom: 24px;
	}

	.card-content {
		display: flex;
		flex-direction: column;
	}

	@media (min-width: 768px) {
		.card-content {
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
		}
	}

	.card-title {
		font-size: 18px;
		font-weight: 600;
		color: #1f2937;
	}

	.card-subtitle {
		display: block;
		color: #6b7280;
		font-size: 14px;
		margin-top: 8px;
	}

	.card-stats {
		display: flex;
		margin-top: 16px;
	}

	@media (min-width: 768px) {
		.card-stats {
			margin-top: 0;
		}
	}

	.stat-item {
		text-align: center;
		margin-left: 20px;
	}

	.stat-item:first-child {
		margin-left: 0;
	}

	.stat-value {
		font-size: 24px;
		font-weight: bold;
		display: block;
	}

	.completed-value {
		color: #4f46e5;
	}

	.inprogress-value {
		color: #4b5563;
	}

	.stat-label {
		font-size: 12px;
		color: #6b7280;
	}

	/* 任务列表标题 */
	.task-list-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 16px;
	}

	.task-list-title {
		font-size: 18px;
		font-weight: 600;
		color: #1f2937;
	}

	.task-list-sort {
		font-size: 14px;
		color: #6b7280;
	}

	/* 任务列表 */
	.task-list {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}

	.task-item {
		background-color: #fff;
		padding: 20px;
		border-radius: 12px;
		border: 1px solid #e5e7eb;
		display: flex;
		align-items: center;
		transition: all 0.3s ease;
	}

	.completed-task {
		background-color: #f0fdf4;
		border-color: #bbf7d0;
	}

	.task-content {
		flex: 1;
		min-width: 0;
	}

	.task-header {
		display: flex;
		align-items: center;
		margin-bottom: 8px;
	}

	.task-title {
		font-size: 16px;
		font-weight: 500;
		color: #1f2937;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.completed-text {
		text-decoration: line-through;
	}

	.badge {
		font-size: 12px;
		border-radius: 12px;
		padding: 2px 8px;
		margin-left: 8px;
		white-space: nowrap;
	}

	.badge-required {
		background-color: #eff6ff;
		color: #3b82f6;
	}

	.badge-done {
		background-color: #ecfdf5;
		color: #10b981;
	}

	.badge-optional {
		background-color: #f5f3ff;
		color: #8b5cf6;
	}

	.task-deadline {
		display: block;
		font-size: 14px;
		color: #6b7280;
		margin-bottom: 12px;
	}

	.task-progress {
		display: flex;
		align-items: center;
		font-size: 14px;
	}

	.progress-bg {
		flex: 1;
		background-color: #f3f4f6;
		border-radius: 4px;
		height: 6px;
		overflow: hidden;
	}

	.progress-fill {
		height: 100%;
		border-radius: 4px;
	}

	.progress-done {
		background-color: #10b981;
	}

	.progress-required {
		background-color: #3b82f6;
	}

	.progress-optional {
		background-color: #8b5cf6;
	}

	.progress-text {
		margin-left: 8px;
		color: #6b7280;
		min-width: 40px;
	}

	/* 按钮样式优化 */
	.check-button {
		width: 48px;
		height: 48px;
		border-radius: 24px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 16px;
		border: none;
		outline: none;
		position: relative;
		overflow: hidden;
		transition: all 0.3s ease;
	}

	/* 未完成状态按钮样式 */
	.check-button:not([disabled]) {
		background: linear-gradient(135deg, #f0f4ff 0%, #e0e7ff 100%);
		box-shadow:
			0 3px 6px rgba(99, 102, 241, 0.15),
			inset 0 -2px 4px rgba(165, 180, 252, 0.5),
			inset 0 2px 4px rgba(255, 255, 255, 0.8);
		border: 1px solid #c7d2fe;
	}

	/* 完成状态按钮样式 */
	.check-button[disabled] {
		background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
		box-shadow:
			0 2px 4px rgba(16, 185, 129, 0.1),
			inset 0 -1px 2px rgba(110, 231, 183, 0.4),
			inset 0 1px 2px rgba(255, 255, 255, 0.8);
		border: 1px solid #6ee7b7;
	}

	/* 按钮点击效果 */
	.check-button:active:not([disabled]) {
		transform: scale(0.95);
		box-shadow:
			0 1px 3px rgba(99, 102, 241, 0.1),
			inset 0 -1px 2px rgba(165, 180, 252, 0.4),
			inset 0 1px 2px rgba(255, 255, 255, 0.8);
	}

	/* 按钮悬停效果 */
	.check-button:not([disabled]):hover {
		transform: translateY(-2px);
		box-shadow:
			0 5px 10px rgba(99, 102, 241, 0.2),
			inset 0 -2px 4px rgba(165, 180, 252, 0.5),
			inset 0 2px 4px rgba(255, 255, 255, 0.9);
	}

	/* 图标样式 */
	.icon {
		font-size: 24px;
		font-weight: bold;
	}

	/* 未完成图标 */
	.icon-check {
		color: #4f46e5;
	}

	/* 完成图标 */
	.icon-check-circle {
		color: #10b981;
	}

	/* 按钮内发光效果 */
	.check-button:not([disabled])::after {
		content: "";
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		border-radius: 50%;
		box-shadow: inset 0 0 8px rgba(79, 70, 229, 0.3);
	}

	.check-button[disabled]::after {
		content: "";
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		border-radius: 50%;
		box-shadow: inset 0 0 8px rgba(16, 185, 129, 0.3);
	}
</style>